
#ifndef SCRIPTFORMAT_H
#define SCRIPTFORMAT_H

#include <BMap>
#include <BRect>
#include <BRange>
#include <BFont>
#include <BTextFormat>
#include <BSource>
#include <BExp>

using namespace BWE;

class SourceFormat : public BTextFormat
{
public:
	struct Item : public BThing
	{
		inline Item(BExp* exp)
		{
			this->exp = exp;
			line = -1;
			error = false;
			if (exp && exp->info.size())
			{
				int fileEnd = exp->info.find(':');
				int lineEnd = exp->info.find('(', fileEnd + 1);
				exp->info.substr(file, fileEnd + 1, lineEnd);
				line = file.toInt();
				if (lineEnd < exp->info.size())
				{
					int begin = lineEnd + 1;
					int end = exp->info.find(')', begin);
					exp->info.substr(file, begin, end);
					int split = file.find(',');
					range.min() = file.toInt();
					file.remove(0, split + 1);
					range.max() = file.toInt();
				}
				exp->info.substr(file, 0, fileEnd);
			}
		}
		BString			file;
		int				line;
		BRange			range;
		bool			error;
		BString			errorText;
		BRect			rect;
		BFontHolder		font;
		BColor			color;
		BExpHolder		exp;
	};
	typedef BHolder<Item>			ItemHolder;
	typedef BMap<int, ItemHolder>	ItemHolderMap;

public:
	SourceFormat();
	~SourceFormat();

	virtual void styled(const BStyle* style) override;
	virtual void changed(const BString& text) override;

	virtual const BFont* font(int pos) const;
	virtual const BColor* color(int pos) const;

	const Item* item(int pos) const;
	Item* insert(BExp* exp);
	void clear();

	const ItemHolderMap& itemMap();

private:
	BMap<int, ItemHolder>	_itemMap;
};

#endif
